package com.globalwave.datasync.convertor.impl;

import java.sql.Timestamp;
import java.sql.Types;

import org.apache.commons.lang.StringUtils;

import com.globalwave.datasync.ConvertorInf;

public class String2Timestamp implements ConvertorInf {

	@Override
	public Object valueOf(Object input) {
		if (input instanceof String) {
			String datetime = ((String) input).trim() ;
			String date = datetime.substring(0, 8) ;
			String time = StringUtils.leftPad(datetime.substring(8), 6, "0") ;
			int year_ = Integer.valueOf(date.substring(0, 4)) ;
			int month_ = Integer.valueOf(date.substring(4, 6)) ;
			int date_ = Integer.valueOf(date.substring(6)) ;

			int hour_ = Integer.valueOf(time.substring(0, 2)) ;
			int min_ = Integer.valueOf(time.substring(2, 4)) ;
			int second_ = Integer.valueOf(time.substring(4)) ;
			
			return new Timestamp(year_ - 1900, month_ - 1, date_, hour_, min_, second_, 0) ;
		} else {
		    return null ;
		}
	}

	@Override
	public int outputType() {
		return Types.TIMESTAMP;
	}
}
